package com.ares.basic.wrap;

import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 响应信息主体
 * 参照mybatis-plus R类
 * @param <T>
 * @author
 */
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class R<T> implements Serializable {
	private static final long serialVersionUID = 1L;

	@Getter
	@Setter
	private int code;

	@Getter
	@Setter
	private String message;


	@Getter
	@Setter
	private T content;

	public Boolean isOk() {
		if(code != 200){
			return false;
		}else{
			return true;
		}
	}

	public static <T> R<T> ok() {
		return restResult(null, ResultEnum.SUCCESS.getCode(), "success");
	}

	public static <T> R<T> ok(T data) {
		return restResult(data, ResultEnum.SUCCESS.getCode(), "success");
	}

	public static <T> R<T> ok(T data, String msg) {
		return restResult(data, ResultEnum.SUCCESS.getCode(), msg);
	}

	public static <T> R<T> failed() {
		return restResult(null, ResultEnum.INTERNAL_SERVER_ERROR.getCode(), "fail");
	}

	public static <T> R<T> failed(String msg) {
		return restResult(null, ResultEnum.INTERNAL_SERVER_ERROR.getCode(), msg);
	}

	public static <T> R<T> failed(T data) {
		return restResult(data, ResultEnum.INTERNAL_SERVER_ERROR.getCode(), ResultEnum.INTERNAL_SERVER_ERROR.getDesc());
	}

	public static <T> R<T> failed(T data, BaseEnum baseEnum) {
		return restResult(data, baseEnum.getCode(), baseEnum.getDesc());
	}

	public static <T> R<T> failed( BaseEnum baseEnum) {
		return restResult(null, baseEnum.getCode(), baseEnum.getDesc());
	}

	public static <T> R<T> failed(T data, int code, String msg) {
		return restResult(data, code, msg);
	}

	public static <T> R<T> failed(int code, String msg) {
		return restResult(null, code, msg);
	}

	private static <T> R<T> restResult(T data, int code, String msg) {
		R<T> apiResult = new R<>();
		apiResult.setCode(code);
		apiResult.setContent(data);
		apiResult.setMessage(msg);
		return apiResult;
	}
}